home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 014 / dex / dex2.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  8KB  |  423 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *            Copyright (c) 1982, Fred Fish            *
  4.  *                All Rights Reserved                *
  5.  *                                    *
  6.  *    This software and/or documentation is released for public    *
  7.  *    distribution for personal, non-commercial use only.        *
  8.  *    Limited rights to use, modify, and redistribute are hereby    *
  9.  *    granted for non-commercial purposes, provided that all        *
  10.  *    copyright notices remain intact and all changes are clearly    *
  11.  *    documented.  The author makes no warranty of any kind with    *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular    *
  14.  *    purpose.                            *
  15.  *                                    *
  16.  ************************************************************************
  17.  */
  18.  
  19.  
  20. /*
  21.  *  FILE
  22.  *
  23.  *    dex2.c   utility routines for DEX modules
  24.  *
  25.  *  KEY WORDS
  26.  *
  27.  *    dex files
  28.  *    utility routines
  29.  *
  30.  *  DESCRIPTION
  31.  *
  32.  *    Contains miscellaneous utility routines which will
  33.  *    eventually show up the the author's "ce" (C Environment)
  34.  *    library, which supplements the stdio library.
  35.  *
  36.  *  FUNCTIONS
  37.  *
  38.  *    nextw     return pointer to next word in a string
  39.  *    skpnbt    skip non-blanks and non-tabs
  40.  *    skpbt     skip blanks and tabs
  41.  *    xfield    extract next field from string
  42.  *    index     find first occurrence of character
  43.  *    rindex    find last occurrence of character
  44.  *
  45.  *  AUTHOR
  46.  *
  47.  *    Fred Fish
  48.  *
  49.  */
  50.  
  51. #include <stdio.h>
  52.  
  53. /*
  54.  *  FUNCTION
  55.  *
  56.  *    NEXTW    return pointer to next word in a string
  57.  *
  58.  *  KEY WORDS
  59.  *
  60.  *    string functions
  61.  *    words
  62.  *    nextw
  63.  *    string pointers
  64.  *
  65.  *  SYNOPSIS
  66.  *
  67.  *    char *nextw(buffer)
  68.  *    char *buffer;
  69.  *
  70.  *  DESCRIPTION
  71.  *
  72.  *    Nextw searches from the current pointer location for the
  73.  *    next occurrence of <tab or space><non-tab and non-space>
  74.  *    which is taken to be the start of a new word.  If found,
  75.  *    a pointer to the <non-tab and non-space> character is
  76.  *    returned. If the end of the string is found, a pointer to it
  77.  *    is returned.
  78.  *
  79.  *  RETURNS
  80.  *
  81.  *    Returns pointer to next word in buffer, if one is found.
  82.  *    Returns pointer to terminating null if no word found.
  83.  *
  84.  *  BUGS
  85.  *
  86.  *    "Words" are taken to be anything which is neither tab nor
  87.  *    blank.  It might be hard to pronounce some of them.
  88.  *    Also note that control characters (ie newline) preceded
  89.  *    by tabs or blanks are also considered to be words.
  90.  *
  91.  */
  92.  
  93. /*
  94.  *  PSEUDO CODE
  95.  *
  96.  *     Begin nextw
  97.  *        Skip over any non-tabs and non-blanks.
  98.  *        Skip over any tabs or blanks.
  99.  *        Return resulting pointer to first char of word.
  100.  *     End nextw
  101.  *                
  102.  */
  103.  
  104. char *nextw(buffer)
  105. char *buffer;
  106. {
  107.     char *skpbt(), *skpnbt();
  108.  
  109.     buffer = skpnbt(buffer);
  110.     buffer = skpbt(buffer);
  111.     return (buffer);
  112. }
  113.  
  114. /*
  115.  *  FUNCTION
  116.  *
  117.  *    skpnbt   return pointer to next tab or blank character
  118.  *
  119.  *  KEY WORDS
  120.  *
  121.  *    string functions
  122.  *    words
  123.  *    skpnbt
  124.  *    string pointers
  125.  *
  126.  *  SYNOPSIS
  127.  *
  128.  *    char *skpnbt(buffer)
  129.  *    char *buffer;
  130.  *
  131.  *  DESCRIPTION
  132.  *
  133.  *    Starting at the current pointer, as long as a NULL is
  134.  *    not encountered, skpnbt skips over anything which is not
  135.  *    a blank or tab, returning a pointer to the first blank
  136.  *    or tab found.  If the end of the string is found first,
  137.  *    a pointer to it is returned.
  138.  *
  139.  *  RETURNS
  140.  *
  141.  *    Returns pointer to next blank or tab character.
  142.  *
  143.  */
  144.  
  145. /*
  146.  *  PSEUDO CODE
  147.  *
  148.  *    Begin skpnbt
  149.  *        If passed point is not NULL then   
  150.  *            While the current char is not a tab, blank, or null
  151.  *            Skip to next character.
  152.  *            End while
  153.  *        End if
  154.  *        Return pointer to the character found.
  155.  *    End skpnbt
  156.  *                
  157.  */
  158.  
  159. char *skpnbt(buffer)
  160. char *buffer;
  161. {
  162.     if (buffer != NULL) {
  163.         while (*buffer != '\t' && *buffer != ' ' && *buffer != '\n') {
  164.         buffer++;
  165.     }
  166.     }
  167.     return (buffer);
  168. }
  169.  
  170. /*
  171.  *  FUNCTION
  172.  *
  173.  *    skpbt    return pointer to next non-tab non-blank character
  174.  *
  175.  *  KEY WORDS
  176.  *
  177.  *    string functions
  178.  *    words
  179.  *    skpbt
  180.  *    string pointers
  181.  *
  182.  *  SYNOPSIS
  183.  *
  184.  *    char *skpbt(buffer)
  185.  *    char *buffer;
  186.  *
  187.  *  DESCRIPTION
  188.  *
  189.  *    Starting at the current pointer, skpbt skips over blanks
  190.  *    and tabs, returning pointer to the first non-tab non-blank
  191.  *    character found (which may be current).
  192.  *
  193.  *  RETURNS
  194.  *
  195.  *    Returns pointer to next non-tab and non-blank character.
  196.  *
  197.  */
  198.  
  199. /*
  200.  *  PSEUDO CODE
  201.  *
  202.  *    Begin skpbt
  203.  *        If pointer is not NULL then
  204.  *            While the current character is a tab or blank
  205.  *            Skip to next character.
  206.  *            End while
  207.  *        End if
  208.  *        Return pointer to the character found.
  209.  *    End skpbt
  210.  *                
  211.  */
  212.  
  213. char *skpbt(buffer)
  214. char *buffer;
  215. {
  216.     if (buffer != NULL) {
  217.         while (*buffer == '\t' || *buffer == ' ') {
  218.         buffer++;
  219.     }
  220.     }
  221.     return (buffer);
  222. }
  223.  
  224. /*
  225.  *  FUNCTION
  226.  *
  227.  *    xfield   extract a field from string
  228.  *
  229.  *  SYNOPSIS
  230.  *
  231.  *    char *xfield(out,in)
  232.  *    char *out;
  233.  *    char *in;
  234.  *
  235.  *  DESCRIPTION
  236.  *
  237.  *    Extracts field from a string up to next tab, blank,
  238.  *    newline, or NULL character.
  239.  *
  240.  *    If the field begins with a double quote then only another
  241.  *    double quote or a NULL will terminate the extraction.
  242.  *
  243.  *  RETURNS
  244.  *
  245.  *    Returns pointer to next character not transfered, or "in"
  246.  *    if no characters are transfered.
  247.  *
  248.  */
  249.  
  250. /*
  251.  *  PSEUDO CODE
  252.  *
  253.  *    Begin xfield
  254.  *        If both pointers are ok then
  255.  *        If the field is quoted then
  256.  *            Discard the leading quote.
  257.  *            While end is not found
  258.  *            Transfer characters.
  259.  *            End while
  260.  *            If terminating character is quote
  261.  *            Discard the terminating quote.
  262.  *            End if
  263.  *            Terminate output string properly.
  264.  *        Else
  265.  *            While end is not found
  266.  *            Transfer characters.
  267.  *            End while
  268.  *            Terminate output string properly.
  269.  *        End if
  270.  *        End if
  271.  *        Return pointer to next input character.
  272.  *    End xfield
  273.  *
  274.  */
  275.  
  276. char *xfield(out,in)
  277. char *out;
  278. char *in;
  279. {
  280.     if (out != NULL && in != NULL) {
  281.     if (*in == '"') {
  282.         in++;
  283.         while (*in != '"' && *in != NULL) {
  284.         *out++ = *in++;
  285.         } 
  286.         if (*in == '"') {
  287.         in++;
  288.         }
  289.         *out = NULL;
  290.     } else {
  291.         while(*in != ' ' && *in != '\t' && *in != NULL && *in != '\n') {
  292.             *out++ = *in++;
  293.         }
  294.         *out = NULL;
  295.     }
  296.     }
  297.     return(in);
  298. }
  299.  
  300. /*
  301.  *  FUNCTION
  302.  *
  303.  *    index   return pointer to occurrence of a character
  304.  *
  305.  *  KEY WORDS
  306.  *
  307.  *    index
  308.  *    character functions
  309.  *
  310.  *  SYNOPSIS
  311.  *
  312.  *    char *index(s,c)
  313.  *    char *s;
  314.  *    char c;
  315.  *
  316.  *  DESCRIPTION
  317.  *
  318.  *    Find first occurrence of a the specified character in
  319.  *    the specified string.
  320.  *
  321.  *  RETURNS
  322.  *
  323.  *    Pointer to character if found.
  324.  *    NULL if no occurrence found.
  325.  *
  326.  */
  327.  
  328. /*
  329.  *  PSEUDO CODE
  330.  *
  331.  *    Begin index
  332.  *        If string pointer valid then
  333.  *            Scan for character or end of string.
  334.  *            If end of string found then
  335.  *            Return NULL.
  336.  *            Else
  337.  *            Return pointer.
  338.  *            End if
  339.  *        Else
  340.  *        Return NULL.
  341.  *        End if
  342.  *    End index
  343.  *
  344.  */
  345.  
  346. char *index(s,c)
  347. char *s;
  348. char c;
  349. {
  350.     if (s != NULL) {
  351.     while (*s != NULL && *s != c) {s++;}
  352.     if (*s == NULL) {
  353.         return(NULL);
  354.     } else {
  355.         return(s);
  356.     }
  357.     } else {
  358.     return(NULL);
  359.     }
  360. }
  361.  
  362. /*
  363.  *  FUNCTION
  364.  *
  365.  *    rindex   return pointer to last occurrence of a character
  366.  *
  367.  *  KEY WORDS
  368.  *
  369.  *    string functions
  370.  *    rindex
  371.  *
  372.  *  SYNOPSIS
  373.  *
  374.  *    char *rindex(s,c)
  375.  *    string *s;
  376.  *    char c;
  377.  *
  378.  *  DESCRIPTION
  379.  *
  380.  *    Find the last occurrence of the specified character in
  381.  *    the specified string.
  382.  *
  383.  *  RETURNS
  384.  *
  385.  *    Pointer to last occurrence if character found.
  386.  *    NULL if character not found.
  387.  *
  388.  */
  389.  
  390. /*
  391.  *  PSEUDO CODE
  392.  *
  393.  *    Begin rindex
  394.  *        Initialize return value to NULL.
  395.  *        If string pointer is not invalid then
  396.  *        For each character in string
  397.  *            If character matches then
  398.  *            Save pointer.
  399.  *            End if
  400.  *        End for
  401.  *        End if
  402.  *        Return pointer.
  403.  *    End rindex
  404.  *
  405.  */
  406.  
  407. char *rindex(s,c)
  408. char *s;
  409. char c;
  410. {
  411.     char *rtn;
  412.  
  413.     rtn = NULL;
  414.     if (s != NULL) {
  415.     for ( ; *s != NULL; *s++) {
  416.         if (*s == c) {
  417.         rtn = s;
  418.         }
  419.     }
  420.     }
  421.     return(rtn);
  422. }
  423.